package com.mindao.app.ued.controller;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.mindao.app.common.base.controller.BaseCoreController;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.common.uploadfile.model.Uploadfile;
import com.mindao.utils.hardinfo.HardInfo;

@Controller
@RequestMapping("/ued/test")
public class TestController extends BaseCoreController {
	private static final Logger log = LoggerFactory
			.getLogger(BaseCoreController.class);

	private static final long serialVersionUID = 7441785390598480063L;
	private int complete = 0;

	// 获取进度值
	public int getComplete() {
		complete += 10;
		return complete;
	}

	public String waitexe(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs) throws Exception{
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String zxing(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		String url = "http:/" + HardInfo.findNonLocalhostIp() + ":"
				+ request.getLocalPort() + request.getContextPath();
		String url_apk = url + "/ea-android.apk";

		int width = 200;
		int height = 200;
		String format = "png";
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		genQR(url_apk, width, height, format, hints, getWebroot()
				+ "/url_apk.png");
		genQR(url, width, height, format, hints, getWebroot() + "/url.png");
		String url_moblie = url + "/app_login.ftl";
		genQR(url_moblie, width, height, format, hints, getWebroot()
				+ "/url_mobile.png");
		rhs.addAttribute("url", url);
		rhs.addAttribute("url_apk", url_apk);
		rhs.addAttribute("url_moblie", url_moblie);
		return super.rtnDefault(request, response, session, rhs);
		// return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	private void genQR(String url, int width, int height, String format,
			Hashtable hints, String filename) throws WriterException,
			IOException {
		BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
				BarcodeFormat.QR_CODE, width, height, hints);

		System.out.println("二维码文件保存地址" + filename);
		File outputFile = new File(filename);
		MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
	}

	@RequestMapping
	public String ckeditor(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs) {
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String menu_para(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		// rhs.addAttribute("system_para_map", infEa.getParaMap());
		// rhs.addAttribute("info_type", "success");
		// rhs.addAttribute("info", "");

		rhs.addAttribute("schools",
				super.systemparaService.getParaList("school"));
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String date(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String dialog_jquery(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String dialog_bootstrap(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String div(HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model rhs) throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String java(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String drag(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String pic(HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model rhs) throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String select_user(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String push_iframe(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		rhs.addAttribute("runtime", Runtime.getRuntime());
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String push_ajax(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		rhs.addAttribute("runtime", Runtime.getRuntime());
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String push(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		rhs.addAttribute("runtime", Runtime.getRuntime());
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String upload(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		request.getRealPath("");
		System.out.println("webroot路径" + request.getRealPath(""));
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String upload_db(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		/* 文件字段会很大，所以这里只取合适的属性 */
		List<List> datalist = baseService
				.find("select new list(id, fileName) from Uploadfile where content != null");
		List<Uploadfile> filelist = new ArrayList<Uploadfile>();
		for (List properties : datalist) {
			Uploadfile file = new Uploadfile();
			System.out.println("id=" + properties.get(0));

			file.setId(Long.valueOf(properties.get(0).toString()));
			file.setFileName((String) properties.get(1));
			filelist.add(file);
		}
		rhs.addAttribute("datalist", filelist);

		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String day_grid(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		List<String> dayList = new ArrayList<String>();
		Date currentDate = new Date();
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("MM-dd");
		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		String daysStr = getpara("showDays");
		String dateStr = getpara("startDate");
		int days = 0;
		if (!"".equals(daysStr))
			days = Integer.parseInt(daysStr);
		else
			days = 10;
		if (!"".equals(dateStr))
			currentDate = df1.parse(dateStr);
		for (int n = 1; n <= days; n++) {
			c.setTime(currentDate);
			c.add(Calendar.DATE, n);
			Date date = c.getTime();
			String s = df.format(date);
			dayList.add(s);
		}
		rhs.addAttribute("startDate",
				"".equals(dateStr) ? df1.format(currentDate) : dateStr);
		rhs.addAttribute("showDays", days);
		rhs.addAttribute("dayList", dayList);
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String mobile(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		return super.rtnDefault(request, response, session, rhs);

	}

	@RequestMapping
	public String pagedata(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs)
			throws Exception {
		getPageData("from Item", rhs);
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String UI_demo(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs) {
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String chart_demo(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs) {
		return super.rtnDefault(request, response, session, rhs);
	}

	@RequestMapping
	public String artDialog(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, Model rhs) {
		return super.rtnDefault(request, response, session, rhs);
	}
	/**
	 * ajax 返回Json数据的例子
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @return
	 */
	@ResponseBody
	@RequestMapping//(method=RequestMethod.POST)
	public JsonResult getTaskNumber(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model rhs ){
		String account=request.getParameter("account");
		System.out.println("account:"+account);
		Integer taskNumber=Double.valueOf(Math.random()*50).intValue();
		return new JsonResult(JsonResult.SUCCESS,"ok",taskNumber);
	}
	/**
	 *  ajax 返回html页面的例子
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @return
	 */
	@RequestMapping
	public String getMsgNumber(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model rhs ){
		int num=Double.valueOf(Math.random()*50).intValue();
		rhs.addAttribute("msgNumber",num);
		return super.rtnDefault(request, response, session, rhs);
	}
}
